package cn.git.demo.service.impl;

import cn.git.demo.dao.OrderDao;
import cn.git.demo.pojo.Order;
import cn.git.demo.pojo.OrderQuery;
import cn.git.demo.service.OrderService;
import cn.itcast.common.page.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service("OrderService")
@Transactional
public class OrderServiceImpl  implements OrderService {

    @Autowired
    private OrderDao orderDao;

    public Pagination selectPaginationByQuery(Long id,Integer paymentWay,Integer isPay,Integer orderState,Integer pageNo,Integer pageSize) {

        //分页逻辑
        OrderQuery orderQuery = new OrderQuery();

        if(pageSize != null) {
            orderQuery.setPageSize(pageSize);
        }
        orderQuery.setPageNo(Pagination.cpn(pageNo));

        //设置分页展示选项
        StringBuffer option = new StringBuffer();

        if (id!=null){
            orderQuery.setId(id);
            option.append("id=").append(id);
        }if (paymentWay!=null){
            orderQuery.setPaymentWay(paymentWay);
            option.append("&paymentWay=").append(paymentWay);
        }else {
            orderQuery.setPaymentWay(null);
            option.append("&paymentWay=").append("");
        }
        if (isPay!=null){
            orderQuery.setIsPay(isPay);
            option.append("&isPay=").append(isPay);
        }else {
            orderQuery.setIsPay(null);
            option.append("&isPay=").append("");
        }if (orderState!=null){
            orderQuery.setOrderState(orderState);
            option.append("&orderState=").append(orderState);
        }else {
            orderQuery.setOrderState(null);
            option.append("&orderState=").append("");
        }
        //创建分页对象 参数：pageNo，pageSize，totalCount
        Pagination pagination = new Pagination(
                orderQuery.getPageNo(),
                orderQuery.getPageSize(),
                orderDao.selectTotalCount(orderQuery)
        );
        List<Order> orderList = orderDao.selectOrderListByQuery(orderQuery);

        pagination.setList(orderList);

        //分页展示

        String url = "/order/list.do";

        pagination.pageView(url,option.toString());

        return pagination;
    }

    public Order selectOrderUserById(Long id) {

        Order order = orderDao.selectOrderUserById(id);
        return order;

    }
    public void addSingleOrder(Order order){
        orderDao.addSingleOrder(order);
    }
    public void updateOrder(Order order){

        orderDao.updateOrder(order);
    }

    public void deleteOrderById(Long id) {
        Order order = orderDao.selectOrderUserById(id);
        if (order.getOrderState() != null){
            if (order.getOrderState()==0){
                orderDao.deleteOrderById(id);
            }else {
                orderDao.updateOrderState(id);
            }
        }else {
            orderDao.deleteOrderById(id);
        }
    }

    public Order selectDetailById(Long id) {

        return orderDao.selectDetailById(id);
    }

}
